package thread.sync.interference;
import static java.lang.System.out;

//synchronizing a class means all static data will be locked for each thread.

public class XYZ_SyncClassObject implements Runnable{
	static int x = 0 , y = 0, z = 0;
	
	public static void main(String[] args) throws InterruptedException{
		
		new Thread(new XYZ_SyncClassObject()).start();
		new Thread(new XYZ_SyncClassObject()).start();
	}
	
	@Override
    public void run() {
		calculate();
	}
	
	static void calculate(){
		synchronized(XYZ_SyncClassObject.class){		
			for (int i=0; i < 100; i++) {
				x++;
				delay();
				y = y + x;
				delay();
				z = z + y;
			}
			out.println("z = " + z);
		}
	}
	
	static void delay(){
		for (double d = 0; d < 1000000; d++) {
			; // A way to delay without sleep, sleep will die this thread for n seconds
		}
	}
}

// Same result in every run
/*
z = 171700
z = 1353400
 */

